#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2023/7/17 11:44
# @Author : 搬砖客
# @File : modify_yaml.py
# @Software: PyCharm


import re
from get_all_files_path import get_all_files
from case_automatic_control import TestCaseAutomaticGeneration
from utils.logging_utils.log_util import write_log

file_func = TestCaseAutomaticGeneration()
date_path = file_func.case_date_path()


def modify_yaml_files(field_name: str, new_value: str):
    """
    修改data文件夹下面的yaml文件的内容
    :param field_name: 需要修改的内容
    :param new_value: 修改后的内容
    :return: None
    """
    file_all = get_all_files(date_path)
    for filepath in file_all:
        # 读取yaml文件内容
        with open(filepath, 'r', encoding="utf-8") as file:
            yaml_content = file.read()

        if not re.search(r'\b{}\b'.format(field_name), yaml_content):
            write_log("提示：{} 不存在于 {} 中".format(field_name, filepath))
            continue

        # 批量替换字符串
        modified_yaml_content = re.sub(r'\b{}\b'.format(field_name), new_value, yaml_content)

        # 将修改后的内容写回yaml文件
        with open(filepath, 'w', encoding="utf-8") as file:
            file.write(modified_yaml_content)
            write_log(f"文件【{filepath}】修改成功，【{field_name}】修改为【{new_value}】")


if __name__ == '__main__':
    modify_yaml_files("assert_sql2", "assert_sql")
    # write_error_log("adsad")
